import os
import numpy as np
from pathlib import Path
from sgfmill import sgf,boards,ascii_boards
doc='./sgfs/'


def list_all_file(fPath):
    fileList=os.listdir(fPath)
    file_list=[]
    for i in fileList:
        file_dir=Path(fPath+'/'+i)
        if file_dir.is_file():
            file_list.append(file_dir)
    return file_list

class sgf_parser():
    def __init__(self,filePath):
        with open(filePath,"rb") as f:
            self.game=sgf.Sgf_game.from_bytes(f.read())
        self.winner=self.game.get_winner()
        self.boardsize=self.game.get_size()
        self.npboard=np.zeros((self.boardsize,self.boardsize))
        self.datas=[]
        self.b=boards.Board(self.boardsize)
    def setSamples(self):
        for node in self.game.get_main_sequence():
            who,point=node.get_move()
            if who is not None and point is not None:
                #b.play(point[0],point[1],who)
                data=(self.npboard,point,1 if who=='b' else -1,1 if self.winner=='b' else -1)    #需要保存的样本，[输入，输出，谁下，谁赢]
                self.datas.append(data)
                self.npboard[point]=1 if who=='b' else -1 #npboard的原点是左上角，sgfmill是左下角，所以生成的数据会是上下镜像

    def saveSamples(self,HDF5,mode=1):  #mode=1只保存胜利方的数据，mode=0保存黑白方的数据
        None

    def showGame(self):
        for node in self.game.get_main_sequence():
            who,point=node.get_move()
            if who is not None and point is not None:
                self.b.play(point[0],point[1],who)
        print(ascii_boards.render_board(self.b))

if __name__ == "__main__":
    doc='./sgfs/'
    file_lists=list_all_file(doc)
    for i in file_lists:
        one_sgf=sgf_parser(i)
        one_sgf.setSamples()
        one_sgf.showGame()
        '''
        for i in one_sgf.datas:
            print("point:",i[1])
        '''
